package com.JZ.QM.controller;



import com.JZ.QM.common.entity.CombinationBaseInfoEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.feign.CombinationFeignService;
import com.JZ.QM.service.CombinationYieldService;
import com.JZ.QM.service.NewCombinationInfoService;
import com.JZ.QM.service.NewCombinationShareService;
import com.JZ.QM.service.NewCombinationYieldService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 * @author cyc
 * @email cyc@gmail.com
 * @date 2020-06-22 22:19:36
 */
@RefreshScope
@RestController
@Slf4j
@RequestMapping("/combinationYield")
public class CombinationYieldController {
    @Resource
    CombinationYieldService combinationYieldService;
    @Resource
    NewCombinationYieldService newCombinationYieldService;
    @Resource
    NewCombinationInfoService newCombinationInfoService;
    @Resource
    CombinationFeignService combinationFeignService;
    @Resource
    NewCombinationShareService newCombinationShareService;

    /**
     * 获取组合的基本信息与用户修改后的组合的基本信息
     * @param comBaseId 组合的基本信息
     * @return
     */
    @RequestMapping("/combinationYieldByCombinationId")
    public CommonResult getAllCombinationYieldByCombinationId(Long comBaseId){
        //组合收益率实体
        HashMap<String, Object> combinationResult;
        //组合基本信息
        LinkedHashMap comBaseInfo;
        //组合的名字
        String comName="";
        //组合的日平均收益率
        BigDecimal comAverage=new BigDecimal(0);
        ArrayList res=new ArrayList();

        //获取组合基本信息
        comBaseInfo= (LinkedHashMap) combinationFeignService.getComBaseInfo(comBaseId).get("combination");
        if (comBaseInfo == null){
            return CommonResult.error(201,"该组合不存在");
        } else {
            //返回组合的收益率信息
            combinationResult=combinationYieldService.getCombinationYields(comBaseId);
            if ( combinationResult.isEmpty()){
                return CommonResult.error(201,"此组合不存在");
            } else {
                comName="原组合，";
                comName+=combinationFeignService.getCombinationShareNamesById(comBaseId).get("newComDetail");
                combinationResult.put("comName",comName);
                //获取用户修改后的组合日平均收益率
                comAverage=combinationYieldService.getComAverageDailyRadio(comBaseId);
                //获取用户修改后的组合信息
                res=newCombinationYieldService.getNewCombinationDailyRadio(comBaseId,comAverage, (String[]) combinationResult.get("newProTradeDate"));
                res.add(combinationResult);
                return CommonResult.success().put("combinationYields",res.toArray());

            }
        }
    }

    /**
     * 根据组合id返回组合的收益信息
     * @param comBaseId 组合的id
     * @return
     */
    @RequestMapping("/comBaseIncome")
    public CommonResult getComBaseInfoIncome(Long comBaseId){
        //原组合信息实体
        LinkedHashMap combinationBaseInfoEntity =new LinkedHashMap();
        //组合的收益信息
        HashMap comIncomes = new HashMap();
        //返回结果集
        CommonResult commonResult=CommonResult.success();

        combinationBaseInfoEntity= (LinkedHashMap)combinationFeignService.getComBaseInfo(comBaseId).get("combination");
        if (combinationBaseInfoEntity.isEmpty()){
            return CommonResult.error(201,"该组合不存在");
        } else {
            commonResult.put("comBaseName",combinationBaseInfoEntity.get("comBaseName"));
            comIncomes=combinationYieldService.getCombinationYields(comBaseId);
            commonResult.put("comIncome",comIncomes.get("newComIncome"));
            commonResult.put("proTradeDate",comIncomes.get("newProTradeDate"));
            return commonResult;
        }

    }

}
